import json
import numpy as np
file = open('../data/result.csv','a',encoding='utf-8')



class DataSet(object):
    config_name = ''
    cudnn_kernel_time = []
    cudnn_total_time = []
    convfft_cpu_time = []
    convfft_kernel_time = []
    convfftv2_cpu_time = [] 
    convfft_total_time = []
    convfft_v3_total_time = []
    convfftv2_cpu_transreal2comp = []
    convfftv2_cpu_fft_cuda_2d_fwd= []
    convfftv3_gpu_fft_cuda_2d_fwd= []
    convfftv2_cpu_vectorcrossmul= []
    convfftv2_cpu_fft_cuda_2d_bwd= []
    convfftv2_cpu_matrixaddition= []
    convfftv2_cpu_matrixtrimpad= []
    accrate_list = []

    def __init__(self, config_name):
        self.config_name = config_name
        self.cudnn_kernel_time = []
        self.cudnn_total_time = []
        self.convfft_cpu_time = []
        self.convfftv2_cpu_time = []
        self.convfft_kernel_time = []
        self.convfft_total_time = []
        self.convfft_v3_total_time = []
        self.accrate_list = []
        self.convfftv2_cpu_transreal2comp = []
        self.convfftv2_cpu_fft_cuda_2d_fwd= []
        self.convfftv3_gpu_fft_cuda_2d_fwd= []
        self.convfftv2_cpu_vectorcrossmul= []
        self.convfftv2_cpu_fft_cuda_2d_bwd= []
        self.convfftv2_cpu_matrixaddition= []
        self.convfftv2_cpu_matrixtrimpad= []

with open('../result.txt','r',encoding='utf-8') as f:
    map = {}
    current_config = ''
    for line in f.readlines():
        if "Config: " in line:
            if (map.get(line) is None):
                map[line] = DataSet(line)
            current_config = line
        if "The cudnnConvolutionForward run time is: " in line and current_config :
            map.get(current_config).cudnn_kernel_time.append(float(line.split('The cudnnConvolutionForward run time is: ')[1].replace('ms\n','')))
        if "The cudnn_forward run time is: " in line:
            map.get(current_config).cudnn_total_time.append(float(line.split('The cudnn_forward run time is: ')[1].replace('ms\n','')))
        if "The convfft_2d run time is: " in line:
            map.get(current_config).convfft_cpu_time.append(float(line.split('The convfft_2d run time is: ')[1].replace('ms\n','')))
        if "The convfft_2d_v2 run time is: " in line:
            map.get(current_config).convfftv2_cpu_time.append(float(line.split('The convfft_2d_v2 run time is: ')[1].replace('ms\n','')))
        if "The convfft_2d_kernel run time is: " in line:
            map.get(current_config).convfft_kernel_time.append(float(line.split('The convfft_2d_kernel run time is: ')[1].replace('ms\n','')))
        if "The convfft_2d_gpu run time is: " in line:
            map.get(current_config).convfft_total_time.append(float(line.split('The convfft_2d_gpu run time is: ')[1].replace('ms\n','')))
        if "The convfft_2d_gpu_v3 run time is: " in line:
            map.get(current_config).convfft_v3_total_time.append(float(line.split('The convfft_2d_gpu_v3 run time is: ')[1].replace('ms\n','')))
        if "The transReal2CompWithMemset run time is: " in line:
            map.get(current_config).convfftv2_cpu_transreal2comp.append(float(line.split("The transReal2CompWithMemset run time is: ")[1].replace('ms\n','')))
        if "The fft_cuda_2d run time is: " in line:
            map.get(current_config).convfftv2_cpu_fft_cuda_2d_fwd.append(float(line.split('The fft_cuda_2d run time is: ')[1].replace('ms\n','')))
        if "The fft_cuda_2d in convfft_gpu_v3 run time is: " in line:
            map.get(current_config).convfftv3_gpu_fft_cuda_2d_fwd.append(float(line.split('The fft_cuda_2d in convfft_gpu_v3 run time is: ')[1].replace('ms\n','')))
        if "The vectorCrossMultip run time is: " in line:
            map.get(current_config).convfftv2_cpu_vectorcrossmul.append(float(line.split('The vectorCrossMultip run time is: ')[1].replace('ms\n','')))
        if "The fft_cuda_2d inverse run time is: " in line:
            map.get(current_config).convfftv2_cpu_fft_cuda_2d_bwd.append(float(line.split('The fft_cuda_2d inverse run time is: ')[1].replace('ms\n','')))
        if "The matrixAddition run time is: " in line:
            map.get(current_config).convfftv2_cpu_matrixaddition.append(float(line.split('The matrixAddition run time is: ')[1].replace('ms\n','')))
        if "The matrixTrimPadding and transreal run time is: " in line:
            map.get(current_config).convfftv2_cpu_matrixtrimpad.append(float(line.split('The matrixTrimPadding and transreal run time is: ')[1].replace('ms\n','')))
        # if "Error when N is" in line:
        #     map.get(current_config).accrate_list.append(line.split('Error when N is')[len(line.split['Error when N is']) - 1].replace('\n',''))

    for key,i in enumerate(map):
        value = map.get(i)
        ## result = i.replace('\n','') + \
        result = ',' + str(round(np.mean(np.array(value.cudnn_total_time[-9:])),2)) + \
        ',' + str(round(np.mean(np.array(value.convfftv2_cpu_time[-9:])),2))  + \
        ',' + str(round(np.mean(np.array(value.convfft_v3_total_time[-9:])),2)) 

        # print(value.cudnn_kernel_time[-9:])
        # print(value.cudnn_kernel_time)
        # print(value.cudnn_total_time)
        # print(value.convfft_cpu_time)
        # print(value.convfft_kernel_time)
        # print(value.convfft_total_time)

        print(result)

        
